/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI;

using SCS.Web.UI.WebControls.Design;

namespace SCS.Web.UI.WebControls
{
	//[TypeConverter(typeof(ItemCssClassConverter))]
	public class ItemClass : IStateManager
	{
		private bool isTrackingViewState;
		private StateBag viewState;

		private string cssClass = string.Empty;
		private string cssClassOver = string.Empty;
		private string cssClassSelected = string.Empty;
		private string cssClassSelectedOver = string.Empty;
		private string cssClassDisabled = string.Empty;

		public ItemClass()
		{
		}

		public ItemClass(string cssClass, string cssClassOver, string cssClassSelected)
		{
			this.cssClass = cssClass;
			this.cssClassOver = cssClassOver;
			this.cssClassSelected = cssClassSelected;
		}

		public ItemClass(string cssClass, string cssClassOver, string cssClassSelected, string cssClassSelectedOver, string cssClassDisabled)
		{
			this.cssClass = cssClass;
			this.cssClassOver = cssClassOver;
			this.cssClassSelected = cssClassSelected;
			this.cssClassSelectedOver = cssClassSelectedOver;
			this.cssClassDisabled = cssClassDisabled;
		}

		
		[Bindable(true), Category("Appearance"), DefaultValue(""), Description("CSS class to apply for the normal state of the menu item.")]
		[NotifyParentProperty(true)]
		public string Normal
		{
			get { return this.cssClass; }
			set { this.cssClass = value; }
		}

		[Bindable(true), Category("Appearance"), DefaultValue(""), Description("CSS class to apply to menu item for mouse-over effect.")]
		[NotifyParentProperty(true)]
		public string Over
		{
			get { return this.cssClassOver; }
			set { this.cssClassOver = value; }
		}

		[Bindable(true), Category("Appearance"), DefaultValue(""), Description("CSS class to apply for the selected menu item.")]
		[NotifyParentProperty(true)]
		public string Selected
		{
			get { return this.cssClassSelected; }
			set { this.cssClassSelected = value; }
		}

		[Bindable(true), Category("Appearance"), DefaultValue(""), Description("CSS class to apply for the selected menu item mouse-over effect.")]
		[NotifyParentProperty(true)]
		public string SelectedOver
		{
			get { return this.cssClassSelectedOver; }
			set { this.cssClassSelectedOver = value; }
		}

		[Bindable(true), Category("Appearance"), DefaultValue(""), Description("CSS class to apply for disabled menu items.")]
		[NotifyParentProperty(true)]
		public string Disabled
		{
			get { return this.cssClassDisabled; }
			set { this.cssClassDisabled = value; }
		}
	
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
		public string Name
		{
			get 
			{ 
				return "ItemClass";
			}
		}
		

//		public string SaveToString()
//		{
//			return string.Format("N:{0} O:{1} S:{2} SO:{3} D:{4}", 
//				this.Normal, this.Over, this.Selected, this.SelectedOver, this.Disabled);
//		}
//
//		public void LoadFromString(string s)
//		{
//			string[] values = s.Split(new char[]{':', ' '});
//
//			this.Normal = values[1];
//			this.Over = values[3];
//			this.Selected = values[5];
//			this.SelectedOver = values[7];
//			this.Disabled = values[9];
//		}

		
		protected StateBag ViewState 
		{
			get 
			{
				if (viewState == null) 
				{ 
					viewState = new StateBag(false);
					if (isTrackingViewState) ((IStateManager)viewState).TrackViewState();
				}
				return viewState;
			}
		}

		internal void SetDirty() 
		{
			if (viewState != null) 
			{
				ICollection Keys = viewState.Keys;
				foreach (string key in Keys) 
				{
					viewState.SetItemDirty(key, true);
				}
			}
		}

		
		public override string ToString() 
		{
			return GetType().Name; 
		}


		#region IStatemanager implementation
		
		bool IStateManager.IsTrackingViewState 
		{
			get 
			{
				return isTrackingViewState;
			}
		}

		void IStateManager.LoadViewState(object savedState) 
		{
			if (savedState != null) 
			{
				((IStateManager)ViewState).LoadViewState(savedState);

				string className = (string)ViewState["CssClass"];
				
				if (className != null)
					this.cssClass = className;

				className = (string)ViewState["CssClassOver"];
				
				if (className != null)
					this.cssClassOver = className;

				className = (string)ViewState["CssClassSelected"];
				
				if (className != null)
					this.cssClassSelected = className;

				className = (string)ViewState["CssClassSelectedOver"];
				
				if (className != null)
					this.cssClassSelectedOver = className;

				className = (string)ViewState["CssClassDisabled"];
				
				if (className != null)
					this.cssClassSelectedOver = className;
			}
		}

		object IStateManager.SaveViewState() 
		{  
			string currentClass = this.cssClass;
			string initialClass = (string)ViewState["CssClass"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["CssClass"] = currentClass;
			}
		
			currentClass = this.cssClassOver;
			initialClass = (string)ViewState["CssClassOver"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["CssClassOver"] = currentClass;
			}

			currentClass = this.cssClassSelected;
			initialClass = (string)ViewState["CssClassSelected"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["CssClassSelected"] = currentClass;
			}

			currentClass = this.cssClassSelectedOver;
			initialClass = (string)ViewState["CssClassSelectedOver"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["CssClassSelectedOver"] = currentClass;
			}

			currentClass = this.cssClassDisabled;
			initialClass = (string)ViewState["CssClassDisabled"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["CssClassDisabled"] = currentClass;
			}

			if (viewState != null) 
			{
				return ((IStateManager)viewState).SaveViewState();
			}
			return null;
		 }

		void IStateManager.TrackViewState() 
		{
			if (this.cssClass.Length > 0)
				ViewState["CssClass"] = this.cssClass;

			if (this.cssClassOver.Length > 0)
				ViewState["CssClassOver"] = this.cssClassOver;
			
			if (this.cssClassSelected.Length > 0)
				ViewState["CssClassSelected"] = this.cssClassSelected;
			
			if (this.cssClassSelectedOver.Length > 0)
				ViewState["CssClassSelectedOver"] = this.cssClassSelectedOver;
			
			if (this.cssClassDisabled.Length > 0)
				ViewState["CssClassDisabled"] = this.cssClassDisabled;

			isTrackingViewState = true;

			if (viewState != null) 
			{
				((IStateManager)viewState).TrackViewState();
			}
		}

		
		#endregion
	}
}
